package preorderlayer

import (
    "errors"
	"encoding/json"
    "dhfshop/models/preorder"
	"github.com/astaxie/beego"
)


func OnOnceDayTick(args int64) error {
	beego.Debug("OnOnceDayTick:", args)
	resItemId := args
	resItem, err := preorder.GetResItemById(resItemId)
	if err != nil {
	   return err
	}
	templateHub, err := TemplateToHub(resItem.PartnerId, resItem.StoreId, resItem.TemplateId)
	if err != nil {
		return err
	}
	b, _ := json.Marshal(templateHub)
	beego.Debug(string(b))
	defaultReserveDays := 0 
	if opt, err := preorder.GetPreOrderOptByPartnerId(resItem.PartnerId, resItem.StoreId); err == nil {
	    defaultReserveDays = opt.CanReserveDays
	}
	if defaultReserveDays < 1 {
	   err = errors.New("defaultReserveDays is invalid")
	}
	if period, err := ModelToDay(resItemId,templateHub, defaultReserveDays); err == nil {
		preorder.AddPeriods(period)
	    preorder.AddMultiTimeslots(period.Timeslots)
    }
	return nil
}
